package com.zhongxi.guoyuan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.comment.CommentData;

import org.json.JSONObject;

import java.util.List;

/**
 * Created by Administrator on 2016/11/9.
 */

public class CommentAdapter extends BaseAdapter {
    private List<CommentData> list;
    private LayoutInflater mLayoutInflater;
    private JSONObject ary;

    public CommentAdapter(Context context, List<CommentData> data) {
        this.list = data;
        this.mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public CommentData getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();             //item_comment
            convertView = mLayoutInflater.inflate(R.layout.item_evaluate, parent, false);
            holder.rb = (RatingBar) convertView.findViewById(R.id.rb);
            holder.tvPublish = (TextView) convertView.findViewById(R.id.tvPublish);
            holder.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
            holder.tvComment = (TextView) convertView.findViewById(R.id.tvComment);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (null != list.get(position) && !"".equals(list.get(position))) {
            CommentData data = list.get(position);
            if (null != data.getStarlevel() && !"".equals(data.getStarlevel())) {
                holder.rb.setRating(Float.parseFloat(data.getStarlevel()));
            }
            if (null != data.getContent() && !"".equals(data.getContent())) {
                holder.tvComment.setText(data.getContent());
            } else {
                holder.tvComment.setText("");
            }
            if (null != data.getCtime() && !"".equals(data.getCtime())) {
                holder.tvTime.setText(data.getCtime());
            } else {
                holder.tvTime.setText("");
            }
            try {
                String username = "";
                if (null != ary && !"".equals(ary)) {
                    if (null != data.getUserid() && !"".equals(data.getUserid())) {
                        username = ary.optString(data.getUserid());
                    }
                }
                if (!"".equals(username)) {
                    holder.tvPublish.setText(username);
                } else {
                    holder.tvPublish.setText("");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return convertView;
    }

    public void setUser(JSONObject ary) {
        this.ary = ary;
    }

    class ViewHolder {
        private RatingBar rb;
        private TextView tvPublish;
        private TextView tvTime;
        private TextView tvComment;
    }
}
